#include "object.h"
#include "mm.h"
#include "bgc/include/gc.h"
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <string.h>


VALUE * nth(int i, VALUE * cons)
{
  VALUE * tmp = cons;
  int n;
  for(n=0;n<i;n++)
    {
      tmp = Cdr(tmp);
    }
  return Car(tmp);
}

int ListLength (VALUE * cons)
{
  VALUE * interpolator;
  int n=0;

  if(cons->type != TYPE_CONS)
    return 0;

  interpolator = cons;

  while(interpolator->cdr != MMNil() &&
	interpolator->cdr->type == TYPE_CONS)
    {
      interpolator = interpolator->cdr;
      n++;
    }
  return n+1;
}

VALUE * nthConsCell(int i, VALUE * cons)
{
  VALUE * tmp = cons;
  int n;
  for(n=0;n<i;n++)
    {
      tmp = Cdr(tmp);
    }
  return tmp;
}

VALUE * Cast (int to, VALUE * object)
{
	char word_temp[MAX_STRING_SIZE];
	bzero(word_temp,MAX_STRING_SIZE);
	switch(object->type)
	{
		case TYPE_INTEIRO:
			if(to == TYPE_INTEIRO)
				return object;
			else
			if(to == TYPE_CHAR)
				return MMChar(object->i);
			else
			if(to == TYPE_DOUBLE)
				return MMDouble((double) object->i);
			else
			if(to == TYPE_CONS)
				return MMNil();
			else
			if(to == TYPE_WORD)
			  {
				sprintf(word_temp, "%d", object->i);
				return MMWord(word_temp);
			  }
			else
			if(to == TYPE_STRING)
			  {
			    
				sprintf(word_temp, "%d", object->i);
				return MMString(word_temp);
			  }
			else
				{printf("error: unknow cast\n"); return NULL;}
			break;
		case TYPE_CHAR:
			if(to == TYPE_INTEIRO)
			  return MMInteger(object->i);
			else
			if(to == TYPE_CHAR)
				return object;
			else
			if(to == TYPE_DOUBLE)
				return MMDouble((double) object->i);
			else
			if(to == TYPE_CONS)
				return MMNil();
			else
			if(to == TYPE_WORD)
			  {
				sprintf(word_temp, "\"%c\"", (char) object->i);
				return MMWord(word_temp);
			  }
			else
			if(to == TYPE_STRING)
			  {
			    
				sprintf(word_temp, "\"%c\"", (char) object->i);
				return MMString(word_temp);
			  }
			else
				{printf("error: unknow cast\n"); return NULL;}
			break;
		case TYPE_DOUBLE:
			if(to == TYPE_INTEIRO)
				return MMInteger((int) object->d);
			else
			if(to == TYPE_CHAR)
				return MMChar((int) object->d);
			else
			if(to == TYPE_DOUBLE)
				return object;
			else
			if(to == TYPE_CONS)
				return MMNil();
			else
			if(to == TYPE_WORD)
			  {
				sprintf(word_temp, "%d", object->i);
				return MMWord(word_temp);
			  }
			else
			if(to == TYPE_STRING)
			  {
				sprintf(word_temp, "%d", object->i);
				return MMString(word_temp);
			  }
			else
				{printf("error: unknow cast\n"); return NULL;}
			break;
		case TYPE_CONS:
			if(to == TYPE_INTEIRO)
				return MMInteger(0);
			else
			if(to == TYPE_CHAR)
				return MMChar(0);
			else
			if(to == TYPE_DOUBLE)
				return MMDouble(0);
			else
			if(to == TYPE_CONS)
				return object;
			else
			if(to == TYPE_WORD)
				return MMWord("");
			else
			if(to == TYPE_STRING)
				return MMString("");
			else
				{printf("error: unknow cast\n"); return NULL;}
			break;
		case TYPE_STRING:
			if(to == TYPE_INTEIRO)
				return MMInteger(0);
			else
			if(to == TYPE_CHAR)
				return MMChar(0);
			else
			if(to == TYPE_DOUBLE)
				return MMDouble(0);
			else
			if(to == TYPE_CONS)
				return MMNil();
			else
			if(to == TYPE_WORD)
				return MMWord("");
			else
			if(to == TYPE_STRING)
				return object;
			else
				{printf("error: unknow cast\n"); return NULL;}
			break;
		case TYPE_WORD:
			if(to == TYPE_INTEIRO)
				return MMInteger(0);
			else
			if(to == TYPE_CHAR)
				return MMChar(0);
			else
			if(to == TYPE_DOUBLE)
				return MMDouble(0);
			else
			if(to == TYPE_CONS)
				return MMNil();
			else
			if(to == TYPE_WNewfile.hORD)
				return object;
			else
			if(to == TYPE_STRING)
			  return MMString(object->word);
			else
				{printf("error: unknow cast\n"); return NULL;}
			break;
	        default:
                                {printf("error: initial value unkown");}
	}
	return NULL;
}
*/


